package pl.elka.pobr.ppr.logic;

public class UnsharpMaskFilter {

    public static int[] filter(Pixel[][] org, Pixel[][] dst) {
        return filter(org, dst, 0.7f, 2, 2);
    }

    public static int[] filter(Pixel[][] org, Pixel[][] dst, float amount, int radius, int threshold) {
        int width = org.length;
        int height = org[0].length;
        int[] srcPixels = new int[width * height];
        for (int i = 0; i < height; ++i) {
            for (int j = 0; j < width; ++j) {
                srcPixels[i * width + j] = org[j][i].toInt();
            }
        }
        int[] dstPixels = new int[width * height];
        float[] kernel = createGaussianKernel(radius);
        // horizontal pass
        blur(srcPixels, dstPixels, width, height, kernel, radius);
        // vertical pass
        blur(dstPixels, srcPixels, height, width, kernel, radius);
        // blurred image is in srcPixels, we copy the original in dstPixels
        for (int i = 0; i < height; ++i) {
            for (int j = 0; j < width; ++j) {
                dstPixels[i * width + j] = org[j][i].toInt();
            }
        }
        // we compare original and blurred images,
        // and store the result in srcPixels
        sharpen(dstPixels, srcPixels, width, height, amount, threshold);
        return srcPixels;
    }

    private static void sharpen(int[] original, int[] blurred, int width, int height, float amount, int threshold) {
        int index = 0;
        int srcR, srcB, srcG;
        int dstR, dstB, dstG;
        amount *= 1.6f;
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                int srcColor = original[index];
                srcR = (srcColor >> 16) & 0xFF;
                srcG = (srcColor >> 8) & 0xFF;
                srcB = (srcColor) & 0xFF;
                int dstColor = blurred[index];
                dstR = (dstColor >> 16) & 0xFF;
                dstG = (dstColor >> 8) & 0xFF;
                dstB = (dstColor) & 0xFF;
                if (Math.abs(srcR - dstR) >= threshold) {
                    srcR = (int) (amount * (srcR - dstR) + srcR);
                    srcR = srcR > 255 ? 255 : srcR < 0 ? 0 : srcR;
                }
                if (Math.abs(srcG - dstG) >= threshold) {
                    srcG = (int) (amount * (srcG - dstG) + srcG);
                    srcG = srcG > 255 ? 255 : srcG < 0 ? 0 : srcG;
                }
                if (Math.abs(srcB - dstB) >= threshold) {
                    srcB = (int) (amount * (srcB - dstB) + srcB);
                    srcB = srcB > 255 ? 255 : srcB < 0 ? 0 : srcB;
                }
                int alpha = srcColor & 0xFF000000;
                blurred[index] = alpha | (srcR << 16) | (srcG << 8) | srcB;
                index++;
            }
        }
    }

    /**
     * <p>
     * Blurs the source pixels into the destination pixels. The force of the blur is specified by the radius which must be greater
     * than 0.
     * </p>
     * <p>
     * The source and destination pixels arrays are expected to be in the INT_ARGB format.
     * </p>
     * <p>
     * After this method is executed, dstPixels contains a transposed and filtered copy of srcPixels.
     * </p>
     * 
     * @param srcPixels
     *            the source pixels
     * @param dstPixels
     *            the destination pixels
     * @param width
     *            the width of the source picture
     * @param height
     *            the height of the source picture
     * @param kernel
     *            the kernel of the blur effect
     * @param radius
     *            the radius of the blur effect
     */
    private static void blur(int[] srcPixels, int[] dstPixels, int width, int height, float[] kernel, int radius) {
        float a;
        float r;
        float g;
        float b;
        int ca;
        int cr;
        int cg;
        int cb;
        for (int y = 0; y < height; y++) {
            int index = y;
            int offset = y * width;
            for (int x = 0; x < width; x++) {
                a = r = g = b = 0.0f;
                for (int i = -radius; i <= radius; i++) {
                    int subOffset = x + i;
                    if (subOffset < 0 || subOffset >= width) {
                        subOffset = (x + width) % width;
                    }
                    int pixel = srcPixels[offset + subOffset];
                    float blurFactor = kernel[radius + i];
                    a += blurFactor * ((pixel >> 24) & 0xFF);
                    r += blurFactor * ((pixel >> 16) & 0xFF);
                    g += blurFactor * ((pixel >> 8) & 0xFF);
                    b += blurFactor * ((pixel) & 0xFF);
                }
                ca = (int) (a + 0.5f);
                cr = (int) (r + 0.5f);
                cg = (int) (g + 0.5f);
                cb = (int) (b + 0.5f);
                dstPixels[index] = ((ca > 255 ? 255 : ca) << 24) | ((cr > 255 ? 255 : cr) << 16) | ((cg > 255 ? 255 : cg) << 8)
                        | (cb > 255 ? 255 : cb);
                index += height;
            }
        }
    }

    static float[] createGaussianKernel(int radius) {
        if (radius < 1) {
            throw new IllegalArgumentException("Radius must be >= 1");
        }
        float[] data = new float[radius * 2 + 1];
        float sigma = radius / 3.0f;
        float twoSigmaSquare = 2.0f * sigma * sigma;
        float sigmaRoot = (float) Math.sqrt(twoSigmaSquare * Math.PI);
        float total = 0.0f;
        for (int i = -radius; i <= radius; i++) {
            float distance = i * i;
            int index = i + radius;
            data[index] = (float) Math.exp(-distance / twoSigmaSquare) / sigmaRoot;
            total += data[index];
        }
        for (int i = 0; i < data.length; i++) {
            data[i] /= total;
        }
        return data;
    }
}